package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.MembershipLevels;
import com.example.mall.dto.MembershipLevelsDto;
import com.example.mall.mapper.MembershipLevelsMapper;
import com.example.mall.query.MembershipLevelsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.IMembershipLevelsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 会员等级表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class MembershipLevelsServiceImpl extends ServiceImpl<MembershipLevelsMapper, MembershipLevels> implements IMembershipLevelsService {

    @Autowired
    private MembershipLevelsMapper membershipLevelsMapper;

    @Override
    public PageDTO<MembershipLevelsDto> queryPage(MembershipLevelsQuery membershipLevelsQuery) {
        if (membershipLevelsQuery == null) {
            membershipLevelsQuery = new MembershipLevelsQuery();
        }
        Page<MembershipLevels> page = membershipLevelsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<MembershipLevels> cartPage = lambdaQuery()
                .like(membershipLevelsQuery.getLevelId()!= null, MembershipLevels::getLevelId, membershipLevelsQuery.getLevelId())
                .like(membershipLevelsQuery.getName()!= null, MembershipLevels::getName, membershipLevelsQuery.getName())
                .like(membershipLevelsQuery.getDescription()!= null, MembershipLevels::getDescription, membershipLevelsQuery.getDescription())
                .like(membershipLevelsQuery.getPointsRequired()!= null, MembershipLevels::getPointsRequired, membershipLevelsQuery.getPointsRequired())
                .like(membershipLevelsQuery.getDiscountRate()!= null, MembershipLevels::getDiscountRate, membershipLevelsQuery.getDiscountRate())
                .like(membershipLevelsQuery.getStatus()!= null, MembershipLevels::getStatus, membershipLevelsQuery.getStatus())
                .orderByDesc(MembershipLevels::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, MembershipLevelsDto.class);
    }

    @Override
    public MembershipLevels queryMembershipLevelsById(Long levelId) {
        AssertUtil.isNotNull(levelId, "levelId不能为空");
        return membershipLevelsMapper.selectById(levelId);
    }

    @Override
    public List<MembershipLevels> queryMembershipLevelsList() {
        return this.list();
    }

    @Override
    public void insert(MembershipLevelsDto membershipLevelsDto) {
        MembershipLevels membershipLevels = new MembershipLevels();
        CopyUtils.copyProperties(membershipLevelsDto, membershipLevels);
        membershipLevels.setLevelId(CodeGenerateUtils.generateUniqueId());
        membershipLevels.setCreateDate(new Date());
        membershipLevels.setUpdateDate(new Date());
        membershipLevels.setStatus(BaseConstants.EFFECTIVE_STATUS);
        membershipLevelsMapper.insert(membershipLevels);
    }

    @Override
    public void update(MembershipLevelsDto membershipLevelsDto) {
        AssertUtil.isNotNull(membershipLevelsDto.getLevelId(), "levelId不能为空");
        MembershipLevels membershipLevels = membershipLevelsMapper.selectById(membershipLevelsDto.getLevelId());
        AssertUtil.isNotNull(membershipLevels, "会员等级不存在");
        CopyUtils.copyProperties(membershipLevelsDto, membershipLevels);
        membershipLevels.setUpdateDate(new Date());
        membershipLevelsMapper.updateById(membershipLevels);
    }

    @Override
    public void delete(Long levelId) {
        AssertUtil.isNotNull(levelId, "levelId不能为空");
        MembershipLevels membershipLevels = membershipLevelsMapper.selectById(levelId);
        AssertUtil.isNotNull(membershipLevels, "会员等级不存在");
        membershipLevels.setStatus(BaseConstants.LOSE_STATUS);
        membershipLevels.setUpdateDate(new Date());
        membershipLevelsMapper.updateById(membershipLevels);
    }

    @Override
    public void deleteBatch(List<Long> levelIds) {
        AssertUtil.isNotNull(levelIds, "levelIds不能为空");
        List<MembershipLevels> membershipLevels = membershipLevelsMapper.selectBatchIds(levelIds);
        AssertUtil.isNotNull(membershipLevels, "会员等级不存在");
        for (MembershipLevels membershipLevel : membershipLevels) {
            membershipLevel.setStatus(BaseConstants.LOSE_STATUS);
            membershipLevel.setUpdateDate(new Date());
            membershipLevelsMapper.updateById(membershipLevel);
        }
    }
}
